﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PlayListSet.Comm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PlayListSet.Entity
{
    public class AppConfig : System.ComponentModel.INotifyPropertyChanged
    {
        [SqlSugar.SugarColumn(IsPrimaryKey = true)]
        public int ID { get; set; } = 1;
        public bool LTCEnable { get; set; }
        private string ltcMp3= "ltc/LTC_00000000_120mins_30fps_44100x24.mp3";
        [SqlSugar.SugarColumn(ColumnDescription = "LTC音频文件")]

        public string LTCMp3
        {
            set { ltcMp3 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("LTCMp3")); }
            get => ltcMp3;
        }
        private string ltcAudioDevice = string.Empty;
        public string LTCAudioDevice
        {
            set { ltcAudioDevice = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("LTCAudioDevice")); }
            get => ltcAudioDevice;
        }
        private string localAudioDevice = string.Empty;
        public string LocalAudioDevice
        {
            set { localAudioDevice = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("LocalAudioDevice")); }
            get => localAudioDevice;
        }
        //<!--参考时间，无法作为timecode用-->
        [SqlSugar.SugarColumn(ColumnDescription="发送osc")]

        public bool OSCSend { get; set; } = true;
        [SqlSugar.SugarColumn(ColumnDescription = "远程osc网络信息 ip:port")]

        public string OSCSendNetInfo { get; set; } = "172.16.1.199:18801";
        [SqlSugar.SugarColumn(ColumnDescription = "本地osc网络信息 ip:port")]

        public string OSCLocalNetInfo { get; set; } = "172.16.1.199:18831";
        [SqlSugar.SugarColumn(ColumnDescription = "UDP控制端口")]

        public int UdpPort { get; set; } = 9988;
        public int ApiPort { get; set; } = 9989;
        public int Vol { get; set; } = 50;
        public event PropertyChangedEventHandler PropertyChanged;

        public static void Save(AppConfig  appConfig)
        {
            //var json = Newtonsoft.Json.JsonConvert.SerializeObject(appConfig, Formatting.Indented);
            //System.IO.File.WriteAllText(
            //    System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "cfg", "appConfig.json"),
            //    json, Encoding.UTF8);
            var cfg = AppHelp.GetAppSqlSugarClient().Queryable<AppConfig>().First();
            if (cfg == null)
            {
                 AppHelp.GetAppSqlSugarClient().Insertable<AppConfig>(appConfig).ExecuteCommand();
            }
            else
            {
                AppHelp.GetAppSqlSugarClient().Updateable<AppConfig>(appConfig).ExecuteCommand();
            }
        }
        public static AppConfig Read()
        {
            var cfg = AppHelp.GetAppSqlSugarClient().Queryable<AppConfig>().First();
            if (cfg == null)
            {
               cfg = new AppConfig();
                AppHelp.GetAppSqlSugarClient().Insertable<AppConfig>(cfg).ExecuteCommand();
            }
            return cfg;
            //var text = System.IO.File.ReadAllText(
            //     System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "cfg", "appConfig.json"), Encoding.UTF8);
            //return Newtonsoft.Json.JsonConvert.DeserializeObject<AppConfig>(text);
        }
    }
}
